/**
 * @Project : ProgrammingExercises
 * @Package : ForOffer
 * @File : Q006.java
 * @Author : WangRuoyu
 * @Date 2022/12/27 10:57
 */

package foroffers;


import util.ListNode;

import java.util.Arrays;

public class Q0006 {
    public int[] reversePrint(ListNode head) {
        if (head == null) {
            return new int[0];
        }
        int count = 0;

        ListNode p = null;
        ListNode s = null;

        while (head != null) {
            s = head.next;
            head.next = p;
            p = head;
            head = s;
            count++;
        }

        int[] res = new int[count];

        for (int i = 0; i < count; i++) {
            res[i] = p.val;
            p = p.next;
        }

        return res;
    }

    public static void main(String[] args) {
        Q0006 sl = new Q0006();
        ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
        int[] res = sl.reversePrint(head);
        System.out.println(Arrays.toString(res));
    }
}

